// ? 获取input
let ipts = document.querySelectorAll(".userCard__log-box input");

let isUsername = false;
let isPassword = false;

// ? 用户名
ipts[0].onchange = function () {
  let username = ipts[0].value;
  let reg = /^\w{3,15}$/;
  if (reg.test(username)) {
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    isUsername = true;
  } else {
    this.nextElementSibling.innerHTML = "格式不正确";
    this.nextElementSibling.style.color = "red";
    isUsername = false;
  }
};

// ? 密码
ipts[1].onchange = function () {
  let password = ipts[1].value;
  let reg = /^.{3,18}$/;
  if (reg.test(password)) {
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    isPassword = true;
  } else {
    this.nextElementSibling.innerHTML = "格式不正确";
    this.nextElementSibling.style.color = "red";
    isPassword = false;
  }
};

// ? 登录
ipts[2].onclick = function(){
    if(isUsername && isPassword){
        let username = ipts[0].value;
        let password = ipts[1].value;
        /* 
        登录接口

     接口地址：http://jx.xuzhixiang.top/ap/api/login.php
     接口请求方式：get
     接口参数：
          username用户名
          password密码
     使用方式
          http://jx.xuzhixiang.top/ap/api/login.php?username=11&password=1212

     服务器返回json数据
        */
        let logApi = "http://jx.xuzhixiang.top/ap/api/login.php";
        axios.get(logApi,{params:{username,password}}).then(r=>{
            console.log(r.data);
            if(r.data.code === 1){
                alert(r.data.msg);

                // ! 设置本地存储
                localStorage.setItem("id",r.data.data.id);

                localStorage.setItem("token",r.data.data.token);

                location.href = "index.html";
            }
        })
    }
}